package com.lx.algorithm.structure;

/**
 * Description: 用数组实现队列（先进先出）
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-02 16:32:11
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-02     张李鑫                     1.0         1.0 Version
 */
public class RingArray {
    private int defaultLimit = 10;
    private int[] arr = null;
    private int [] minArr=null;

    private int pushIndex=0;

    private int popIndex=0;

    private int size=0;

    public RingArray() {
        arr = new int[defaultLimit];
        minArr = new int[defaultLimit];
    }

    public RingArray(int defaultLimit) {
        this.defaultLimit=defaultLimit;
        arr = new int[defaultLimit];
        minArr = new int[defaultLimit];
    }

    public void push(int value){
        if (size<defaultLimit) {
            arr[pushIndex] = value;
            if (size==0){
                minArr[pushIndex]=value;
            }else if (minArr[pushIndex-1]>value){
                minArr[pushIndex]=value;
            }else {
                minArr[pushIndex]= minArr[pushIndex-1];
            }
            pushIndex++;
            size++;
        }else {
            System.out.println("队列满了");
        }
    }

    public int  getMin(){
        return minArr[pushIndex-1];
    }

    public int pop(){
        if (size==0){
            System.out.println("队列为空");
            return -1;
        }
        size--;
        int value =arr[popIndex];
        popIndex++;
        return value;
    }

    public int getIndex(int index){
        return index<defaultLimit-1?index++:0;
    }


}
